﻿var $oss={
	upgeturl:'/upload/fileupload',
	updir:'',
	setUpDir:function(updir){
		$oss.updir=updir;
	},getUpDir:function(){
		return $oss.updir;
	},getSignature:function(){
	  var obj = $oss.sendRequest();
      var data=new Object();
      data.host=obj.host;
      data.policy=obj.policy;
      data.accessid=obj.accessid;
      data.signature=obj.signature;
      data.callback=obj.callback;
      data.dir=obj.dir;
      return data;
	},getUploadfilename:function(filename){
		var suffix = $oss.getSuffix(filename);
		var object_name=new Date().getTime()+$oss.randomString(4) + suffix;
		return object_name;
		
	},randomString:function(len) {
	　　len = len || 32;
	　　var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';   
	　　var maxPos = chars.length;
	　　var pwd = '';
	　　for (i = 0; i < len; i++)
		{pwd += chars.charAt(Math.floor(Math.random() * maxPos));}
		return pwd;
	},getSuffix:function(filename) {
	    var pos = filename.lastIndexOf('.')
	    var suffix = ''
	    if (pos != -1) {
	        suffix = filename.substring(pos)
	    }
	    return suffix;
	},setUploadParam:function(up,filename){
		var new_multipart_params = {
	        
	    };
	    up.setOption({
	        'url': ctx+$oss.upgeturl+"?updir="+$oss.updir,
	        'multipart_params': new_multipart_params
	    });
	},initUpload:function(uploaderparm){
		var beforeUpload=uploaderparm.init.BeforeUpload;
		if(!uploaderparm.init.Error)uploaderparm.init.Error=$oss._plError;
		uploaderparm.init.BeforeUpload=function(up, file){
			$oss.setUploadParam(up, file.name);
			if(beforeUpload)beforeUpload(up, file);
		}
		var uploader = new plupload.Uploader(uploaderparm);
		
		uploader.init();
		return uploader;
	},_plError:function(up, err){
		if (err.code == -600) alert("选择的文件太大了");
        else if (err.code == -601)alert("选择的文件类型不对");
        else alert("Error code:" +err.code);
	},previewImageOne:function(file,callback){
		if(!file || !/image\//.test(file.type)) return;
		if(file.type=='image/gif'){
			var fr2 = new mOxie.FileReader();
			fr2.onload = function(){
				callback(fr2.result);
				fr2.destroy();
				fr2 = null;
			}
			fr2.readAsDataURL(file.getSource());
		}else{
			var preloader = new mOxie.Image();
			preloader.onload = function() {
				//preloader.downsize( 300, 300 );
				var imgsrc = preloader.type=='image/jpeg' ? preloader.getAsDataURL('image/jpeg',80) : preloader.getAsDataURL(); //得到图片src,实质为一个base64编码的数据
				callback && callback(imgsrc);
				preloader.destroy();
				preloader = null;
			};
			preloader.load( file.getSource() );
		}
	},sendRequest:function(){
		var serverUrl = ctx+$oss.upgeturl+'?updir='+$oss.getUpDir();
		var result="";
		$.ajax({
			type: 'GET',
			url:serverUrl,
			data:{},
			dataType:"json",
			cache: false,
			async:false,
			success: function(ret){result =ret},
			error:function(){result="";}
		});
	    return result;
	}
}